package com.example.yml0612;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.yuanmenglong0612.R;

import java.util.List;

public class LvAdapter extends BaseAdapter {

    List<Bean.DataBean.PhonesBean> phones;
    Context context;

    public LvAdapter(List<Bean.DataBean.PhonesBean> phones, Context context) {
        this.phones = phones;
        this.context = context;
    }

    @Override
    public int getCount() {
        return phones.size();
    }

    @Override
    public Object getItem(int position) {
        return phones.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    class ViewHolder {
        ImageView img;
        TextView brand;
        TextView name;
        TextView price;


    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item, null);
            viewHolder = new ViewHolder();
            viewHolder.img = convertView.findViewById(R.id.img);
            viewHolder.brand = convertView.findViewById(R.id.brand);
            viewHolder.name = convertView.findViewById(R.id.name);
            viewHolder.price = convertView.findViewById(R.id.price);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        Bean.DataBean.PhonesBean phonesBean = phones.get(position);
        viewHolder.img.setImageResource(phonesBean.getImage());
        viewHolder.brand.setText(phonesBean.getBrand());
        viewHolder.name.setText(phonesBean.getName());
        viewHolder.price.setText("约￥" + phonesBean.getPrice());

        return convertView;
    }
}
